<template>
    <div class="app-teach">
        <div class="head">
            <el-avatar :size="120" :src="simg"></el-avatar>
            <p>{{sname}}</p>
            <RouterLink :to="`/addpost`" ><el-button class="add" type="primary"> 发布帖子</el-button></RouterLink>
            
        </div>
      
        <div class="app-course">
            <h1>我的贴吧<br></h1>
      <div class="nav">
        <div class="nav-left">
          <div class="time">
            <span>时间</span>
            <div class="tubiao">
              <el-icon @click="getPostListByOrder('create_time','asc')"><CaretTop /></el-icon>
              <el-icon @click="getPostListByOrder('create_time','desc')"><CaretBottom /></el-icon>
            </div>

          </div>
          <div class="hot">
            <span>热度</span>
            <div class="tubiao">
              <el-icon @click="getPostListByOrder('replies','asc')"><CaretTop /></el-icon>
              <el-icon @click="getPostListByOrder('replies','desc')"><CaretBottom /></el-icon>
            </div>
          </div>
        </div>

        <el-form-item label="">
          <el-input v-model="keyword" placeholder="请输入贴吧名称" style="width: auto"/>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
     
      <ul>
        <li v-for="item in PostList" :key="item.id">
          <RouterLink :to="`/post/${item.id}`">
          <div class="title">{{item.title}}</div>
          <div class="content">{{item.content}}</div>
          <div class="bottom">
            <span><el-icon><UserFilled /></el-icon>{{item.userName}}</span>
            <span>{{dateFormat(item.createTime)}}</span>
            
          </div>
          <div class="recount">
            <el-icon class="ping"><ChatDotRound /></el-icon>
            <span>{{item.replies}}</span>
          </div>
          </RouterLink>
          <div class="del">
            <el-button type="primary" @click="delPost(item.id)">删除</el-button>
          </div>
        
        </li>
        
      </ul>
      <div class="example-pagination-block">
        <el-pagination
            :current-page="pageNum"
            :page-size="pageSize"
            :background="true"
            layout=" prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
            style="margin-top: 15px"
        />
      </div>
      <!-- <RouterLink :to="`/addpost`" ><el-button class="add" type="primary"> 发布帖子</el-button></RouterLink> -->
   </div>
    </div>
</template>
<script setup>

import { useRouter } from 'vue-router';
import userApi  from '@/apis/post'
import {ref, onMounted, watch, computed, onUpdated, reactive} from 'vue'
import {useRoute} from 'vue-router'
import {findUserAPI} from '@/apis/home'
const router = useRoute()

let PostList = ref([])
let pageNum = ref(1)
let pageSize = ref(5)
let total = ref(0)
let orderBy = ref('create_time')
let order = ref('desc')
let keyword = ref('')
let sname=ref('')
let simg=ref('')


function getPostList(val)  {
  console.log(pageNum.value)
  pageNum=val;
  console.log("page,orderBy,order=",pageNum,orderBy.value,order.value)
  userApi.myPost(pageNum,order.value,orderBy.value,keyword.value).then((res)=>{
      console.log("res==",res)
      PostList.value = res.data.list ;
      console.log(PostList.value)
    
      total.value = res.data.total;
      pageSize.value=res.data.pageSize;
      pageNum=res.data.pageNum;
    })
}

const getPostListByOrder = (order_by,orderr) => {
  orderBy.value = order_by
  order.value = orderr
  getPostList(pageNum.value)
}
const getUserInfo = async () => {
    try { 
      
        const res = await findUserAPI();
        sname.value = res.data.data.name
        simg.value = res.data.data.avatar
       
        return res;
        
    } catch (error) {
        console.error(error);
    }
}
onMounted(() => {
    getUserInfo()
    orderBy.value='replies'
    order.value='desc'
    getPostList(pageNum.value);
});

const handleCurrentChange = (val) => {
          getPostList(val)
        };
onMounted(()=>{
    const el = document.querySelectorAll('.nav-left .el-icon')
    console.log("el==",el)
    el.forEach(item=>{
        item.addEventListener('click',()=>{
             const isActive = item.classList.contains('active')
             el.forEach(icon => icon.classList.remove('active'))
            if (!isActive) {
            item.classList.add('active') // 添加所点击图标的激活状态
      }

        })
    })
})

function onSearch() {
  let key = keyword.value.trim();
  console.log("params =", key);
  if(key==""){
    handleCurrentChange(pageNum.value);
  }else{
    userApi.myPost(pageNum,order.value,orderBy.value,keyword.value).then(response => {
      console.log("response =", response);
      PostList.value= response.data.list;
      pageNum=response.data.pageNum;
      pageSize.value=response.data.pageSize;
      total.value=response.data.total
      console.log("PostList.data =", PostList);
    })
  }
}

function dateFormat(time) {
      var date = new Date(time);
      var year = date.getFullYear();
      
      var month =
        date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    //   var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      // 拼接
      return year + "-" + month + "-" + day + " "+ hours + ":" +minutes;
    }
  const delPost = async(id)=>{
    await userApi.deletePost(id)
    location.reload()
  }


</script>
<style scoped>

.app-teach {
    width: 990px;
    margin: 0 auto;
    background-color: #fff;
    position: relative;
    margin-bottom: 100px;
    padding-bottom: 60px;
}
.app-course h1{
  margin-left: 20px;
  padding-bottom: 5px;
  border-bottom: 1px solid #000;
  margin-bottom: 20px;
}
.app-course ul{
  margin-bottom: 70px;
}
.head{
    height: 200px;
    /* background-color: rgb(92,92,92); */
    margin-top: 20px;
    display: flex;
    padding: 20px 30px;
    align-items: center;
    /* border-bottom: 1px solid #000; */
}
.head p {
    /* color: #fff; */
    margin-left: 30px;
    font-size: 26px;
}
.body{
    padding: 20px 20px;
}
  table {
      border-collapse: collapse;
      width: 100%;
    }
    th, td {
      border: 1px solid black;
      padding: 8px;
      text-align: left;
    }
    th {
      background-color: #f2f2f2;
    }
    tr:nth-child(even) {
      background-color: #dddddd;
    }
    .form-group {
      margin-bottom: 10px;
    }
    .form-group label {
      display: block;
      font-weight: bold;
    }
    .form-group input[type="text"] {
      width: 300px;
    }
    .form-group input[type="submit"] {
      padding: 5px 10px;
      background-color: #4CAF50;
      color: white;
      border: none;
      cursor: pointer;
    }
.subBtn{
 margin-left: 180px;
}
#addbut{
  margin-top: 20px;

}

 ul{
    display: flex;
    flex-wrap: wrap;
    margin-top: 10px;
   
}

ul li {
  box-shadow: 0px 0px 0px rgba(0,0,0,0.2);
  transition: box-shadow 0.3s;
  border:1px solid #EBEEF5;
}

ul li:hover {
  box-shadow: 3px 5px 10px rgba(0,0,0,0.3);
}
 ul li{
    list-style: none;
    width: 205px;
    height: 240px;
    margin-right: 18px;
      /* border: 1px solid #000; */
    background-color: #fff;
     border-radius: 5px;
     margin-bottom: 30px;
}
ul li:nth-child(5n){
    margin-right: 0;
}
li img{
    width: 100%;
    height: 120px;
     border-radius: 5px;

}
li .count{
    margin-top: 15px;
    display: flex;
    color:#999;
    /* margin-bottom: 5px; */
    font-size: 14px;
   
}
li .datatime {
    color:#999;
    font-size: 14px;
}
li .count p{
    margin-right: 20px;
}
li .miaoshu{
    padding: 0 10px;
}
li .depart{
    font-size: 14px;
    color:#ccc;

}
.dot {
    display: inline-block;
    width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .app-course{
   
    width: 900px;
    margin: 0 auto;
}
.time, .hot{
    display: inline-block;
    display: flex;
}
.nav{
    display: flex;
    justify-content: space-between;
    padding: 20px ;
}
.nav-left{
    display: flex;
}
.time{
    margin-right: 10px;
}
.tubiao{
    display: flex;
    flex-direction: column;
    justify-content: flex-start ;

}
.tubiao .el-icon:nth-child(2){
    position: relative;
    top:-6px;
}
.nav input{
    width: 120px;
    height: 29px;
    border: none;
    outline: none;
    padding: 4px 6px;
}


ul li {
  box-shadow: 0px 0px 0px rgba(0,0,0,0.2);
  transition: box-shadow 0.3s;
  border:1px solid #EBEEF5;
}

ul li:hover {
  box-shadow: 3px 5px 10px rgba(0,0,0,0.3);
}
 ul li{
    list-style: none;
    width: 900px;
    height: 180px;
    background-color: #fff;
     border-radius: 5px;
     margin-bottom: 10px;
     padding: 30px;
     position: relative;
}



.dot {
    display: inline-block;
    width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}


.example-pagination-block .example-demonstration {
  margin-bottom: 16px;

}
.example-pagination-block {
  width: 200px;
  height: 50px;
  margin: auto;
}
.el-icon{
    cursor: pointer;
}
.nav-left .active{
    color: red;
}
.add{
  margin-top: 20px;
  margin-bottom: 50px;
  margin-left: 30px;
}
li .title{
  font-size: 20px;
  margin-bottom: 18px;
}

.content{
  width: 100%;
  height: 50px;
  word-break: break-all;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2; /* 这里是超出几行省略 */

}
.bottom{
  position: absolute;
  bottom: 10px;
}
span{
  margin-right: 12px;
}
.el-icon{
  margin-right: 4px;

}
.recount{
  position: absolute;
  right: 30px;
  bottom: 10px;
}
.recount span{
  vertical-align: top;
  font-size: 13px;
}
.add{
  position: absolute;
  top:68px;
}
.del{
  position: absolute;
  top:30px;
  right:30px;
}
</style>